
// 首页内容
<template>
  <div class="box-item">
    <!-- 顶部搜索框 -->
    <!-- <header class="home-header wrap" :class="{ active: headerScroll }"> -->
    <header class="home-header wrap active">
      <router-link tag="i" to="./category"
        ><i class="nbicon nbmenu2" style="display: none"></i
      ></router-link>
      <div class="header-search">
        <span class="app-name">百合商城</span>
        <i class="iconfont icon-search"></i>
        <router-link
          tag="span"
          class="search-title"
          to="./product-list?from=home"
          >山河无恙，人间皆安</router-link
        >
      </div>
      <router-link class="login" tag="span" to="./login" v-if="!isLogin"
        >登录</router-link
      >
      <router-link class="login" tag="span" to="./user" v-else>
        <van-icon style="display: none" name="manager-o" />
      </router-link>
    </header>

    <nav-bar></nav-bar>
    <!-- 轮播图 -->
    <swiper style="margin-top: 45px" :list="swiperList"></swiper>

    <!-- icon导航 -->
    <div class="category-list">
      <div
        v-for="item in categoryList"
        v-bind:key="item.categoryId"
        @click="navlist(item)"
      >
        <img :src="item.imgUrl" />
        <div>{{ item.name }}</div>
      </div>
    </div>

    <!-- 拼团专区 秒杀活动 -->
    <div class="nav-center">
      <div class="nav-item-1" @click="navitem">拼团专区</div>

      <!-- <router-link class="nav-item-2" to="../views/HomePro/Seckill"
        >秒杀活动</router-link
      > -->

      <router-link class="nav-item-2" to="/Seckill">秒杀活动</router-link>
      <!-- <div class="nav-item-2">秒杀活动</div> -->
    </div>

    <!-- 商品分类 -->
    <div class="good">
      <header class="good-header">热销榜</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="item in newGoodses"
          :key="item.goodsId"
          @click="goToDetail(item)"
        >
          <img :src="prefix(item.goodsCoverImg)" alt="" />
          <div class="good-desc">
            <!-- <div class="title">{{ item.goodsName }}</div> -->
            <div class="title">{{ item.goodsIntro }}</div>

            <div class="pri-1">
              <div class="price">¥ {{ item.sellingPrice }}</div>
              <div class="ys">¥15000</div>
            </div>

            <div>
              已销售数量：
              <span>1000</span>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="good">
      <header class="good-header">热门商品</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="item in hots"
          :key="item.goodsId"
          @click="goToDetail(item)"
        >
          <img :src="prefix(item.goodsCoverImg)" alt="" />
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="good" :style="{ paddingBottom: '100px' }">
      <header class="good-header">最新推荐</header>
      <div class="good-box">
        <div
          class="good-item"
          v-for="item in recommends"
          :key="item.goodsId"
          @click="goToDetail(item)"
        >
          <img :src="prefix(item.goodsCoverImg)" alt="" />
          <div class="good-desc">
            <div class="title">{{ item.goodsName }}</div>
            <div class="price">¥ {{ item.sellingPrice }}</div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// 底部导航组件
import navBar from "@/components/NavBar";
// 轮播图组件
import swiper from "@/components/Swiper";
// 首页数据--轮播图、热门商品、最近推荐
import { getHome } from "../service/home";
import { getUserInfo } from "../service/user";
// 模拟数据
import { getLocal } from "@/common/js/utils";
// 弹窗组件
import { Toast } from "vant";
export default {
  name: "home",
  data() {
    return {
      swiperList: [], //轮播图数据
      isLogin: false,
      headerScroll: false,
      hots: [],
      newGoodses: [],
      recommends: [],
      categoryList: [
        {
          id: 1,
          name: "百合超市",
          imgUrl: "http://s.weituibao.com/1583585285461/cs.png",
          categoryId: 100001,
        },
        {
          id: 2,
          name: "百合服饰",
          imgUrl: "http://s.weituibao.com/1583585285468/fs.png",
          categoryId: 100003,
        },

        {
          id: 3,
          name: "百合生鲜",
          imgUrl: "http://s.weituibao.com/1583585285472/sx.png",
          categoryId: 100004,
        },
        {
          id: 4,
          name: "百合到家",
          imgUrl: "http://s.weituibao.com/1583585285467/dj.png",
          categoryId: 100005,
        },
        {
          id: 5,
          name: "充值缴费",
          imgUrl: "http://s.weituibao.com/1583585285465/cz.png",
          categoryId: 100006,
        },
        {
          id: 6,
          name: "全球购",
          imgUrl: "http://s.weituibao.com/1583585285470/qq.png",
          categoryId: 100002,
        },
        // {
        //   id: 7,
        //   name: "9.9元拼",
        //   imgUrl: "http://s.weituibao.com/1583585285469/pt.png",
        //   categoryId: 100007,
        // },
        // {
        //   id: 7,
        //   name: "领劵",
        //   imgUrl: "http://s.weituibao.com/1583585285468/juan.png",
        //   categoryId: 100008,
        // },
        // {
        //   id: 8,
        //   name: "省钱",
        //   imgUrl: "http://s.weituibao.com/1583585285471/sq.png",
        //   categoryId: 100009,
        // },
        // {
        //   id: 9,
        //   name: "全部",
        //   imgUrl: "http://s.weituibao.com/1583585285470/qb.png",
        //   categoryId: 100010,
        // },
      ],
    };
  },
  components: {
    navBar,
    swiper,
  },
  async mounted() {
    const token = getLocal("token");
    if (token) {
      this.isLogin = true;
    }
    window.addEventListener("scroll", this.pageScroll);
    // 弹窗提示
    Toast.loading({
      message: "加载中...",
      forbidClick: true,
    });
    const { data } = await getHome();

    // 获取轮播图数据
    this.swiperList = data.carousels;
    // 新品上线
    this.newGoodses = data.newGoodses;
    // 热门商品
    this.hots = data.hotGoodses;
    // 最新推荐
    this.recommends = data.recommendGoodses;
    //数据请求结束，清除弹窗
    Toast.clear();
    // console.log("商品数据", data.newGoodses);
  },
  methods: {
    // icon列表页面导航
    navlist(item) {
      // if (item.id === 0) {
      //   this.$router.push({
      //     path: "/iconlist", //秒杀活动
      //   });
      // }
      if (item.id === 1) {
        this.$router.push({
          path: "/iconlist-1", //秒杀活动
        });
      }
      if (item.id === 2) {
        this.$router.push({
          path: "/iconlist-2", //秒杀活动
        });
      }
      if (item.id === 3) {
        this.$router.push({
          path: "/iconlist-3", //秒杀活动
        });
      }
      if (item.id === 4) {
        this.$router.push({
          path: "/iconlist-4", //秒杀活动
        });
      }
      if (item.id === 5) {
        this.$router.push({
          path: "/iconlist-5", //秒杀活动
        });
      }
      if (item.id === 6) {
        this.$router.push({
          path: "/iconlist-6", //秒杀活动
        });
      }
      if (item.id === 7) {
        this.$router.push({
          path: "/iconlist-7", //秒杀活动
        });
      }
      if (item.id === 8) {
        this.$router.push({
          path: "/iconlist-8", //秒杀活动
        });
      }

      // this.$router.push({
      //   path: "/iconlist", //秒杀活动
      // });
    },

    //拼团专区页面
    navitem() {
      this.$router.push({
        // path: "../views/HomePro/GroupBooking", //1、跳转的页面路径
        // name: "GroupBooking",//2、跳转的页面路径
        path: "/GroupBooking", //3、跳转的页面路径
      });
    },

    pageScroll() {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop > 100
        ? (this.headerScroll = true)
        : (this.headerScroll = false);
    },
    goToDetail(item) {
      this.$router.push({ path: `product/${item.goodsId}` });
    },
  },
};
</script>

<style lang="less" scoped >
@import "../common/style/mixin";
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  .wh(100%, 50px);
  .fj();
  line-height: 50px;
  padding: 0 15px;
  .boxSizing();
  font-size: 15px;
  color: #fff;
  z-index: 10000;
  .nbmenu2 {
    color: @primary;
  }
  &.active {
    background: @primary;
    .nbmenu2 {
      color: #fff;
    }
    .login {
      color: #fff;
    }
  }

  .header-search {
    display: flex;
    .wh(74%, 20px);
    line-height: 20px;
    margin: 10px 0;
    padding: 5px 0;
    color: #232326;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 20px;
    .app-name {
      padding: 0 10px;
      color: @primary;
      font-size: 18px;
      font-weight: bold;
      border-right: 1px solid #666;
    }
    .icon-search {
      padding: 0 10px;
      font-size: 17px;
    }
    .search-title {
      font-size: 12px;
      color: #666;
      line-height: 21px;
    }
  }
  .icon-iconyonghu {
    color: #fff;
    font-size: 22px;
  }
  .login {
    color: @primary;
    line-height: 52px;
    .van-icon-manager-o {
      font-size: 20px;
      vertical-align: -3px;
    }
  }
}
// <!-- icon导航 -->

// .category-list {
//   border-top: 1px solid #e9e9e9;
//   display: flex;
//   flex-shrink: 0;
//   flex-wrap: wrap;
//   width: 100%;
//   padding-bottom: 13px;
//   div {
//     display: flex;
//     flex-direction: column;
//     width: 20%;
//     text-align: center;
//     img {
//       .wh(40px, 40px);
//       margin: 13px auto 8px auto;
//     }
//   }
// }

.category-list {
  // background: #f9f9f9;
  display: flex;
  overflow-x: scroll;
  border-top: 1px solid #e9e9e9;

  div {
    padding: 10px 0;
    // background: pink;
    width: 80px;
    // height: 80px;
    border-right: 1px solid #e9e9e9;

    text-align: center;

    img {
      .wh(40px, 40px);
      // margin: 1px auto 8px auto;
    }
  }
}

// <!-- 拼团专区 秒杀活动 -->
.nav-center {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  // background: #f9f9f9;
  height: 50px;
  line-height: 50px;
  text-align: center;
  color: @primary;
  font-size: 16px;
  font-weight: 500;
  border: 1px solid #e9e9e9;

  .nav-item-1 {
    box-sizing: border-box;
    width: 50%;
    border-right: 1px solid #e9e9e9;
  }
  .nav-item-2 {
    box-sizing: border-box;
    width: 50%;
    color: #1baeae;
  }
}

.good {
  .good-header {
    background: #f9f9f9;
    border-bottom: 1px solid #e9e9e9;

    height: 50px;
    line-height: 50px;
    text-align: center;
    color: @primary;
    font-size: 16px;
    font-weight: 500;
  }
  .good-box {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    .good-item {
      box-sizing: border-box;
      width: 50%;
      border-bottom: 1px solid #e9e9e9;
      padding: 10px 10px;
      img {
        display: block;
        width: 120px;
        margin: 0 auto;
      }
      .good-desc {
        // text-align: center;
        font-size: 14px;
        padding: 10px 0;
        .title {
          color: #222333;
          line-height: 25px;
        }
        .pri-1 {
          display: flex;
          line-height: 40px;

          // justify-content: space-between;
          .ys {
            margin-left: 10px;
            text-decoration: line-through;
            // margin-top: 3px;
            // line-height: 30px;
          }
        }
        .price {
          color: @primary;
          font-size: 18px;
        }
      }
      // 这里解释一下 &:nth-child(2n + 1) ，首先 & 代表当前类名下，2n + 1 代表奇数，合起来就是当前类名下的奇数子节点。
      &:nth-child(2n + 1) {
        border-right: 1px solid #e9e9e9;
      }
    }
  }
}
.floor-list {
  width: 100%;
  padding-bottom: 50px;
  .floor-head {
    width: 100%;
    height: 40px;
    background: #f6f6f6;
  }
  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    .boxSizing();
    .floor-category {
      width: 50%;
      padding: 10px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      .boxSizing();
      &:nth-child(2n) {
        border-right: none;
      }
      p {
        font-size: 17px;
        color: #333;
        &:nth-child(2) {
          padding: 5px 0;
          font-size: 13px;
          color: @primary;
        }
      }
      .floor-products {
        .fj();
        width: 100%;
        img {
          .wh(65px, 65px);
        }
      }
    }
  }
}
</style>
